﻿namespace Sitecore.ChangesTracker.Commands
{
  using System;
  using System.Collections.Generic;
  using System.Collections.Specialized;
  using System.Linq;
  using System.Text;
  using Sitecore.Data.Archiving;
  using Sitecore.Diagnostics;
  using Sitecore.Shell.Framework.Commands;
  using Sitecore.Web.UI.Sheer;
  using Sitecore.Web.UI.WebControls;
  using Sitecore.Web.UI.XamlSharp.Continuations;
  using ChangesTracker.Archiving;
  
  public class Empty:Command, ISupportsContinuation

  {
    public override void Execute(CommandContext context)
    {
      Assert.ArgumentNotNull(context, "context");
      NameValueCollection parameters = new NameValueCollection();
      parameters["archivename"] = context.Parameters["archivename"];
      ClientPipelineArgs args = new ClientPipelineArgs(parameters);
      ContinuationManager.Current.Start(this, "Run", args);
    } 

    protected void Run(ClientPipelineArgs args)
    {
      Assert.ArgumentNotNull(args, "args");
      if (args.IsPostBack)
      {
        if (args.Result == "yes")
        {
          Sitecore.Data.Archiving.Archive archive = ArchiveManager.Providers["changetracker"].GetArchive(args.Parameters["archivename"], Context.ContentDatabase);
          ArchiveQuery query = new ArchiveQuery();
          query.OriginalLocation = Context.ContentDatabase.Name + ":%";
          Assert.IsNotNull(archive, typeof(Shell.Framework.Commands.Archive));
          if (!Context.IsAdministrator)
          {
            query.ArchivedBy = Context.User.Name;
          }
          archive.RemoveEntries(query);
          Log.Audit(this, "Empty archive: {0}", new string[] { archive.Name });
          SheerResponse.SetLocation(string.Empty);
        }
      }
      else
      {
        SheerResponse.Confirm("Are you sure you want to permanently delete all these items?");
        args.WaitForPostBack();
      }
    }


  }
}
